<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: NSH <develop43@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_graphical;

use cmf\lib\Plugin;
use think\Config;
use think\db;

class QbtGraphicalPlugin extends Plugin
{

    public $info = [
        'name' => 'QbtGraphical',
        'title' => '统计报表',
        'description' => '图形统计报表',
        'status' => 1,
        'author' => '千佰特',
        'version' => '1.0',
        'demo_url' => 'http://www.qbt8.com',
        'author_url' => 'http://www.qbt8.com',
    ];
    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}graphical_config` (
             `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `name` varchar(255) DEFAULT NULL COMMENT '菜单名称',
              `title` varchar(255) DEFAULT NULL COMMENT '图形标题',
              `field` varchar(255) DEFAULT NULL COMMENT '展示的字段值',
              `table` varchar(60) DEFAULT NULL COMMENT '表名',
              `x_axis_name` varchar(255) DEFAULT NULL COMMENT 'x轴名称',
              `category_name` varchar(255) DEFAULT NULL COMMENT '分类（就是有几类型）',
              `shape` tinyint(2) unsigned DEFAULT NULL COMMENT '图表形状',
              `is_menu` tinyint(3) unsigned DEFAULT NULL COMMENT '是否生成新菜单，0否， 1单独页面，2多个图形合并',
              `is_join` tinyint(1) unsigned DEFAULT NULL COMMENT '是否关联',
              `main_table_id` varchar(60) DEFAULT NULL COMMENT '主表关联ID',
              `assistant_table_id` varchar(255) DEFAULT NULL COMMENT '附表关联id',
              `assistant_table` varchar(255) DEFAULT NULL COMMENT '附表',
              `assistant_table_field` varchar(255) DEFAULT NULL COMMENT '附表查询字段',
              `x_axis_val` tinyint(2) unsigned DEFAULT NULL COMMENT 'X轴选择显示类型 1 显示时间轴，2显示自定义标题',
              `x_axis_time` tinyint(3) unsigned DEFAULT NULL COMMENT 'X_轴选择时间轴，1一周，2一个月',
              `x_axis_field` varchar(255) DEFAULT NULL COMMENT 'X轴时间字段',
              `where` varchar(255) DEFAULT NULL COMMENT '条件',
              PRIMARY KEY (`id`)
            )
        ");
        $data_d_menu = [
            'parent_id' => '121',
            'type' => '1',
            'status' => '1',
            'list_order' => '10000',
            'app' => 'plugin/QbtGraphical',
            'controller' => 'AdminIndex',
            'action' => 'multiple_statistics',
            'param' => '',
            'name' => '多图表统计',
            'icon' => '',
            'remark' => '',
        ];
//        INSERT INTO `qbtcmf`.`qbtcmf_admin_menu` (`id`, `parent_id`, `type`, `status`, `list_order`, `app`, `controller`, `action`, `param`, `name`, `icon`, `remark`) VALUES ('268', '121', '1', '1', '10000', 'plugin/QbtGraphical', 'AdminIndex', 'statistics?name=统计菜单下子菜单数量', '', '统计菜单下子菜单数量', '', '');
        $data_da_menu = [
            'parent_id' => '121',
            'type' => '1',
            'status' => '1',
            'list_order' => '10000',
            'app' => 'plugin/QbtGraphical',
            'controller' => 'AdminIndex',
            'action' => 'statistics?name=统计菜单下子菜单数量',
            'param' => '',
            'name' => '统计菜单下子菜单数量',
            'icon' => '',
            'remark' => '',
        ];
        $data = [[
            'name' => '统计菜单下子菜单数量',
            'title' => '模板',
            'field' => 'NULL',
            'table' => 'admin_menu',
            'x_axis_name' => 'a.name',
            'category_name' => '数量',
            'shape' => '2',
            'is_menu' => '1',
            'is_join' => '1',
            'main_table_id' => 'id',
            'assistant_table_id' => 'parent_id',
            'assistant_table' => 'admin_menu',
            'assistant_table_field' => 'NULL',
            'x_axis_val' => '2',
            'x_axis_time' => '1',
            'x_axis_field' => '',
            'where' => 'a.parent_id=0',
        ],
            [
                'name' => '多图表统计',
                'title' => '统计一段时间用户注册量',
                'field' => 'NULL',
                'table' => 'user',
                'x_axis_name' => '',
                'category_name' => '数量',
                'shape' => '2',
                'is_menu' => '2',
                'is_join' => '0',
                'main_table_id' => '',
                'assistant_table_id' => '',
                'assistant_table' => '',
                'assistant_table_field' => 'NULL',
                'x_axis_val' => '1',
                'x_axis_time' => '2',
                'x_axis_field' => 'create_time',
                'where' => '',
            ],
            [
                'name' => '多图表统计',
                'title' => '统计子菜单',
                'field' => 'NULL',
                'table' => 'admin_menu',
                'x_axis_name' => 'a.name',
                'category_name' => '数量',
                'shape' => '1',
                'is_menu' => '2',
                'is_join' => '1',
                'main_table_id' => 'id',
                'assistant_table_id' => 'parent_id',
                'assistant_table' => 'admin_menu',
                'assistant_table_field' => 'NULL',
                'x_axis_val' => '2',
                'x_axis_time' => '1',
                'x_axis_field' => '',
                'where' => 'a.parent_id=0',
            ],


        ];

        Db::name('admin_menu')->insertGetId($data_d_menu);
        Db::name('graphical_config')->insertAll($data);
        Db::name('admin_menu')->insertGetId($data_da_menu);
        return true;
    }

    // 插件卸载
    public function uninstall()
    {
        $config = Config::get('database');
        $graphical = Db::name('graphical_config')->where('is_menu', 1)->select();
        foreach ($graphical as $item) {
            Db::name('admin_menu')->where('name', $item['name'])->delete();
        }
        Db::name('admin_menu')->where('name', '多图表统计')->delete();
        Db::query("DROP TABLE `{$config['prefix']}graphical_config`");
        return true;
    }


}